package com.Service.impl;

import com.Service.UserPermissionsService;
import com.dao.UserPermissionsMapper;
import com.model.pojo.Menu;
import com.model.pojo.User;
import com.model.pojo.UserPermissions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserPermissionsServiceImpl implements UserPermissionsService {
    @Resource
    private UserPermissionsMapper userPermissionsMapper;

    @Override
    public List<UserPermissions> menuNameOne(String userName) {
        return userPermissionsMapper.menuNameOne(userName);
    }

    @Override
    public List<Menu> menuPath(String userName) {
        return userPermissionsMapper.menuPath(userName);
    }

    @Override
    public List<User> userNames() {
        return userPermissionsMapper.userNames();
    }

    @Override
    public List<UserPermissions> userPermissionsList(String name) {
        return userPermissionsMapper.userPermissionsList(name);
    }

    @Override
    @Transactional
    public boolean updaPermissions(String name, String permissions, String[] menu, HttpSession session) {
        List<UserPermissions> userPermissionsList = userPermissionsMapper.userPermissionsList(name);
        boolean bl = true;
        if (userPermissionsList.size() == 0) {

            for (int i = 0; i < menu.length; i += 2) {
                String menuNameOne = menu[i];
                String menuNametwo = menu[i + 1];
                System.out.println(menuNameOne + "" + menuNametwo + "嘿嘿" + "\n");
                if (i + 2 > menu.length) {
                    break;

                }
                userPermissionsMapper.updaPermissions(name, menuNameOne, menuNametwo);
            }

        } else {
            if (permissions.equals("管理员")) {
                session.setAttribute("guan","管理员");
                bl = true;
            } else {

                userPermissionsMapper.updaNumber(name);
                for (int i = 0; i < menu.length; i += 2) {
                    String menuNameOne = menu[i];
                    String menuNametwo = menu[i + 1];
                    if (i + 2 > menu.length) {
                        break;

                    }
                    userPermissionsMapper.updaPermissions(name, menuNameOne, menuNametwo);
                    bl=true;
                }

            }
        }
        return bl;
    }
}